﻿
using DataWorks_Tools.BasicMethods;
using DataWorks_Tools.HalfHourCalSave.Common.Basic;
using DataWorks_Tools.MappModals.CalInputModals;
using DataWorks_Tools.MappModals.CalSaveModals;
using static DataWorks_Tools.HalfHourCalSave.Common.Basic.EnumClass;

namespace DataWorks_Tools.HalfHourCalSave.BinImort

{

    public static class Tools_BinImport
    {
       
        /// <summary>
        /// 获得半小时速度分布数据
        /// </summary>
        /// <param name="csvimport"></param>
        /// <param name="props"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        public static async Task<calsave_spd_engrpm?> GetHalfHourSpdAsync(ReadfromCSV.CSVImport csvimport, HalfHourCalInputModel props, DisorTimeType option)
        {
            calsave_spd_engrpm? spdbin = null;
            if (props.importspd == 1)
            {
                if (props.chantitle_spd != null)
                {
                    var spdlist = await ReadfromCSV.GetSpecifiedColumnDataAsync(csvimport, props.chantitle_spd);
                    var properties = typeof(calsave_spd_engrpm).GetProperties();
                    spdbin = new calsave_spd_engrpm();
                    List<BinClass> data = new List<BinClass>();
                    if(spdlist?.Count>0)
                    {
                        //判断是哪种模式：时间模式和里程模式
                        switch (option)
                        {
                            case DisorTimeType.Distance:
                                var dislist = await Distance.ReturnSingleDistance(spdlist, csvimport.lists[0]);
                                data = await Bin.DivideBin(spdlist, dislist, 10, 120);
                                spdbin.disortime = (byte)DisorTimeType.Distance;
                                break;
                            case DisorTimeType.Time:
                                data = await Bin.DivideBin(spdlist, 10, 120);
                                spdbin.disortime = (byte)DisorTimeType.Time;
                                break;
                        }
                        foreach (var binClass in data)
                        {
                            var property = properties.FirstOrDefault(p => p.Name.ToLower() == ("bin_" + binClass.bin).ToLower());
                            if (property != null)
                            {
                                property.SetValue(spdbin, binClass.value);
                            }
                        }
                        spdbin.filename = csvimport.name;
                        spdbin.vehicle = props.vehicle;
                        spdbin.project = props.project;
                        spdbin.datadate = Convert.ToDateTime(csvimport.datetimesecond);
                        spdbin.subdirectory = csvimport.subdirectory;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            return spdbin;
        }

        /// <summary>
        /// 获得半小时转速分布数据
        /// </summary>
        /// <param name="csvimport"></param>
        /// <param name="props"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        public static async Task<calsave_spd_engrpm?> GetHalfHourEngRpmAsync(ReadfromCSV.CSVImport csvimport, HalfHourCalInputModel props, DisorTimeType option)
        {
            calsave_spd_engrpm? spdbin = null;
            if (props.importengrpm == 1)
            {
                if (props.chantitle_spd != null)
                {
                    var spdlist = await ReadfromCSV.GetSpecifiedColumnDataAsync(csvimport, props.chantitle_spd);
                    var engrpmlist = await ReadfromCSV.GetSpecifiedColumnDataAsync(csvimport, props.chantitle_engspd);
                    var properties = typeof(calsave_spd_engrpm).GetProperties();
                    spdbin = new calsave_spd_engrpm();
                    List<BinClass> data = new List<BinClass>();
                    if(spdlist?.Count > 0 && engrpmlist?.Count > 0)
                    {
                        switch (option)
                        {
                            case DisorTimeType.Distance:
                                var dislist = await Distance.ReturnSingleDistance(spdlist, csvimport.lists[0]);
                                data = await Bin.DivideBin(engrpmlist, dislist, 500, 6000);
                                spdbin.disortime = (byte)DisorTimeType.Distance;
                                break;
                            case DisorTimeType.Time:
                                data = await Bin.DivideBin(engrpmlist, 500, 6000);
                                spdbin.disortime = (byte)DisorTimeType.Time;
                                break;
                        }
                        foreach (var binClass in data)
                        {
                            var property = properties.FirstOrDefault(p => p.Name.ToLower() == ("bin_" + binClass.bin).ToLower());
                            if (property != null)
                            {
                                property.SetValue(spdbin, binClass.value);
                            }
                        }
                        spdbin.filename = csvimport.name;
                        spdbin.vehicle = props.vehicle;
                        spdbin.project = props.project;
                        spdbin.datadate = Convert.ToDateTime(csvimport.datetimesecond);
                        spdbin.subdirectory = csvimport.subdirectory;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            return spdbin;
        }
    }
}
